package com.jh.gmtv.ui.live;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.jh.gmtv.R;
import com.jh.gmtv.bean.ZhiBoBean;
import com.jh.gmtv.util.GlideCircleTransform;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Mr.J on 2016/10/18.
 */

public class LiveAdapter extends BaseAdapter {
    private List<ZhiBoBean> mData;
    private Context mContext;
    private LayoutInflater mInflater;

    public LiveAdapter(Context context){
        this.mContext = context;
        mInflater = LayoutInflater.from(context);
    }
    public void setLiveDatas(List<ZhiBoBean> list){
        this.mData = list;
    }
    @Override
    public int getCount() {
        return mData != null ? mData.size():0;
    }

    @Override
    public Object getItem(int position) {
        return mData != null ? mData.get(position):0;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        //绑定控件
        if (convertView == null){
            convertView = mInflater.inflate(R.layout.grid_item1, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        //绑定数据
        ZhiBoBean zhiBoBean = mData.get(position);
        holder.tvNick.setText(zhiBoBean.getNick());
        holder.tvTitle.setText(zhiBoBean.getTitle());
        RequestManager glideRequest = Glide.with(mContext);
        Glide.with(mContext).load(zhiBoBean.getThumb()).diskCacheStrategy(DiskCacheStrategy.ALL).placeholder(R.mipmap.ic_launcher).into(holder.ivThumb);
        glideRequest.load(zhiBoBean.getAvatar()).transform(new GlideCircleTransform(mContext)).into(holder.ivAvatar);
        return convertView;
    }
    static class ViewHolder{
        @BindView(R.id.iv_thumb)
        ImageView ivThumb;
        @BindView(R.id.iv_avatar)
        ImageView ivAvatar;
        @BindView(R.id.tv_nick)
        TextView tvNick;
        @BindView(R.id.tv_title)
        TextView tvTitle;
        ViewHolder(View view){
            ButterKnife.bind(this,view);
        }
    }
}
